/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.cabinettype.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.biz.modular.cabinettype.entity.BizCabinetType;
import vip.xiaonuo.biz.modular.cabinettype.param.BizCabinetTypeAddParam;
import vip.xiaonuo.biz.modular.cabinettype.param.BizCabinetTypeEditParam;
import vip.xiaonuo.biz.modular.cabinettype.param.BizCabinetTypeIdParam;
import vip.xiaonuo.biz.modular.cabinettype.param.BizCabinetTypePageParam;
import vip.xiaonuo.biz.modular.cabinettype.service.BizCabinetTypeService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 柜子类型控制器-试剂
 *
 * @author kongxm
 * @date 2022/11/12 22:13
 */
@Api(tags = "柜子类型控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@RequestMapping("/biz/cabinettype/reagent")
@Validated
public class BizCabinetTypeReagentController {

    @Resource
    private BizCabinetTypeService bizCabinetTypeService;

    /**
     * 获取柜子类型分页
     *
     * @author kongxm
     * @date 2022/11/12 22:13
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取柜子类型分页")
    @GetMapping("/page")
    public CommonResult<Page<BizCabinetType>> page(BizCabinetTypePageParam bizCabinetTypePageParam) {
        bizCabinetTypePageParam.setCategory(1);
        Page<BizCabinetType> page = bizCabinetTypeService.page(bizCabinetTypePageParam);
        return CommonResult.data(page);
    }

    /**
     * 添加柜子类型
     *
     * @author kongxm
     * @date 2022/11/12 22:13
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加柜子类型")
    @CommonLog("添加柜子类型")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid BizCabinetTypeAddParam bizCabinetTypeAddParam) {
        bizCabinetTypeAddParam.setCategory(1);
        bizCabinetTypeService.add(bizCabinetTypeAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑柜子类型
     *
     * @author kongxm
     * @date 2022/11/12 22:13
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑柜子类型")
    @CommonLog("编辑柜子类型")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizCabinetTypeEditParam bizCabinetTypeEditParam) {
        bizCabinetTypeService.edit(bizCabinetTypeEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除柜子类型
     *
     * @author kongxm
     * @date 2022/11/12 22:13
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除柜子类型")
    @CommonLog("删除柜子类型")
    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                               CommonValidList<BizCabinetTypeIdParam> bizCabinetTypeIdParamList) {
        bizCabinetTypeService.delete(bizCabinetTypeIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取柜子类型详情
     *
     * @author kongxm
     * @date 2022/11/12 22:13
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取柜子类型详情")
    @GetMapping("/detail")
    public CommonResult<BizCabinetType> detail(@Valid BizCabinetTypeIdParam bizCabinetTypeIdParam) {
        return CommonResult.data(bizCabinetTypeService.detail(bizCabinetTypeIdParam));
    }

    /**
     * 获取柜子类型不分页
     *
     * @author kongxm
     * @date 2022/11/12 22:13
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("获取柜子类型不分页")
    @GetMapping("/list")
    public CommonResult<List<BizCabinetType>> list() {
        return CommonResult.data(bizCabinetTypeService.list(new LambdaQueryWrapper<BizCabinetType>()
                .eq(BizCabinetType::getCategory, 1)
                .eq(BizCabinetType::getDeleteFlag, "NOT_DELETE")));
    }

    /**
     * 上传文件
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @ApiOperationSupport(order = 1)
    @ApiOperation("上传文件")
    @CommonLog("上传文件")
    @PostMapping("/upload")
    public CommonResult<String> upload(@RequestPart("file") MultipartFile file) {
        JSONObject jsonObject;
        try {
            try {
                jsonObject = parseJson(file, "GBK");
            } catch (JSONException e) {
                jsonObject = parseJson(file, "UTF-8");
            }
        } catch (Exception e) {
            return CommonResult.error("无法解析上传文件，请检查文件内容");
        }
        BizCabinetTypeAddParam bizCabinetType = new BizCabinetTypeAddParam();
        bizCabinetType.setModel(jsonObject.getString("cabinetType"));
        bizCabinetType.setName(jsonObject.getString("cabinetDesc"));
        bizCabinetType.setNumberOfDoors(jsonObject.getIntValue("cabinetDoorNumber"));
        bizCabinetType.setSvg(jsonObject.getString("cabinetSvg"));
        bizCabinetType.setDoorIdArray(jsonObject.getJSONArray("cabinetDoorID").stream().map(Object::toString).collect(Collectors.joining(",")));
        bizCabinetType.setDoorDisplayNameArray(jsonObject.getJSONArray("cabinetDoorDisplayName").stream().map(Object::toString).collect(Collectors.joining(",")));
        bizCabinetType.setCategory(1);
        bizCabinetTypeService.add(bizCabinetType);
        return CommonResult.ok();
    }

    private JSONObject parseJson(MultipartFile file, String encoding) throws IOException {
        String content = new String(file.getBytes(), encoding);
        return JSON.parseObject(content);
    }
}
